home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / ParticleDemo.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  3.5 KB  |  154 lines

  1. ' Particle Demo
  2. ' Written by Scott Brosious
  3.  
  4. const width = 200, height = 200
  5.  
  6. const nop = 100    ' Number of particles
  7. const xcntr = 100  ' XCenter
  8. const ycntr = 100  ' YCenter
  9. const speed = 1    ' Speed 
  10.  
  11. dim texture    ' Texture handle
  12. dim a   
  13. dim i 
  14. dim time       ' Current time
  15. dim t1,t2      ' Starting and ending time variables   
  16. dim choice
  17.  
  18. dim xpos(nop)  ' Xposition
  19. dim ypos(nop)  ' Yposition
  20. dim ang(nop)   ' Angles
  21. dim st(nop)    ' Start time
  22. dim et(nop)    ' End time
  23. dim mt(nop)    ' Time to move
  24. dim scale(nop) ' Size of particles
  25. dim size       ' Random sizes
  26. dim alpha#     ' Star transparency. 0 = fully transparent (invisible). 1 = fully solid. 0.5 = half transparent e.t.c.
  27.  
  28. dim red(nop)   ' Red Component
  29. dim green(nop) ' Green Component
  30. dim blue(nop)  ' Blue Component
  31.  
  32. for i = 1 to nop
  33.  
  34. size = int(rnd() % 5) + 5  ' Random Size 5 - 10
  35.  
  36. scale(i) = size ' Put size into array
  37.  
  38. next
  39.  
  40. for i = 1 to nop
  41.  
  42. gosub Inittime
  43.  
  44. st(i) = t1 ' Start time
  45. et(i) = t2 ' End time
  46.  
  47. gosub Angles
  48.  
  49. ang(i) = a  ' Put angle into array
  50.  
  51. next
  52.  
  53. ' Set 2D mode
  54. glMatrixMode (GL_PROJECTION)
  55. glLoadIdentity ()
  56. glOrtho (0, width, 0, height, -1, 1)
  57. glMatrixMode (GL_MODELVIEW)
  58. glDisable (GL_DEPTH_TEST)
  59.  
  60. ' Load in texture assign handle
  61. texture = LoadTexture ("data\star.bmp")
  62.  
  63. ' Enable 2D Texturemapping
  64. glEnable (GL_TEXTURE_2D)
  65.  
  66. ' Translucency, Blending
  67. glBlendFunc(GL_SRC_ALPHA,GL_ONE)
  68. glEnable(GL_BLEND)
  69.  
  70. '  Random Colors
  71. for i = 1 to nop
  72.  
  73. choice = int(rnd() % 7) + 1
  74.  
  75. if choice =  1 then red(i) = 1: green(i) = 0: blue(i) = 0: endif 
  76. if choice =  2 then red(i) = 0: green(i) = 1: blue(i) = 0: endif
  77. if choice =  3 then red(i) = 0: green(i) = 0: blue(i) = 1: endif 
  78. if choice =  4 then red(i) = 1: green(i) = 1: blue(i) = 0: endif
  79. if choice =  5 then red(i) = 0: green(i) = 1: blue(i) = 1: endif 
  80. if choice =  6 then red(i) = 1: green(i) = 0: blue(i) = 1: endif
  81. if choice =  7 then red(i) = 1: green(i) = 1: blue(i) = 1: endif 
  82.  
  83. next               
  84.  
  85. while true
  86.  
  87. ' Clear screen   
  88. glClear (GL_COLOR_BUFFER_BIT)
  89.  
  90. glBindTexture (GL_TEXTURE_2D, texture)
  91.  
  92. glBegin (GL_QUADS)
  93.  
  94. for i = 1 to nop
  95.  
  96. if time > st(i) then mt(i) = mt(i) + 1 :endif ' If current time greater then start time then move it.
  97.  
  98. if mt(i) > et(i) then gosub Refresh :endif ' If movetime greater then endtime start again
  99.  
  100. xpos(i)= xcntr + cosd(ang(i)) * (mt(i) * speed)
  101. ypos(i)= ycntr + sind(ang(i)) * (mt(i) * speed)
  102.             
  103.     if et(i) > 0 then 
  104.         alpha# = 1 - mt(i) * 1.0 / et(i)
  105.     else
  106.         alpha# = 1
  107.     endif
  108.             glColor4f (red(i),green(i),blue(i), alpha#)                ' Color of particle 
  109.             glTexCoord2f (0, 1)                         
  110.             glVertex2f (xpos(i)-scale(i),ypos(i)-scale(i))     ' Top left
  111.  
  112.             glTexCoord2f (0, 0)                         
  113.             glVertex2f (xpos(i)-scale(i),ypos(i)+scale(i))     ' Bottom left
  114.             
  115.             glTexCoord2f (1, 0)                         
  116.             glVertex2f (xpos(i)+scale(i),ypos(i)+scale(i))     ' Bottom right    
  117.  
  118.             glTexCoord2f (1, 1)                         
  119.             glVertex2f (xpos(i)+scale(i),ypos(i)-scale(i))     ' Top right
  120.  
  121. next 
  122.  
  123. glEnd ()
  124.  
  125. ' Display output
  126. SwapBuffers ()
  127.  
  128. time = time + 1 ' Time is always incrementing
  129.  
  130. wend
  131.  
  132. ReFresh:
  133.  
  134. gosub InitTime  
  135. st(i) = t1
  136. et(i) = t2
  137. mt(i) = 0
  138. gosub Angles
  139. ang(i) = a
  140.  
  141. return
  142.  
  143. InitTime:
  144.  
  145. t1 = rnd() % 30  ' When to start
  146. t2 = rnd() % 60  ' When to end
  147.  
  148. return              
  149.  
  150. Angles:
  151.  
  152. a = rnd() % 360  ' Random angle 0 - 360
  153.  
  154. return